<?php
    /**
     * 真题写作管理
     * Created by PhpStorm.
     * User: Administrator
     * Date: 15-6-17
     * Time: 下午2:37
     */
    namespace app\modules\exercise\controllers;

    use yii;
    use app\libs\ApiControl;
    use app\modules\cn\models\ExerciseTopic;
    use app\modules\cn\models\ExerciseLevel;
    use app\modules\cn\models\ExerciseType;
    use app\modules\cn\models\ExerciseCategory;
    use app\modules\exercise\models\ExerciseList;
    use app\modules\exercise\models\ExerciseListDetail;

    class WriteController extends ApiControl {
        public $layout = 'exercise';
        public $enableCsrfValidation = false;

        /**
         * @Title  [口语列表]
         * @Author Don
         * @Date   2021-05-11
         * @return [type]
         */
        public function actionIndex()
        {
            // 题型
            // $option['type'] = ExerciseType::getType(462);
            // // 难度
            // $option['level'] = ExerciseLevel::getLevel(462);
            // // 话题
            // $option['topic'] = ExerciseTopic::getTopic(537);
            // $type = Yii::$app->request->get('type', '');
            // $level = Yii::$app->request->get('level', '');
            // $topic = Yii::$app->request->get('topic', '');
            $page  = Yii::$app->request->get('page',1);
            $cate_id = Yii::$app->request->get('cate_id', '');

            $cate_model = new ExerciseCategory();
            $cate = $cate_model->getAllCates(512);
            $cate_str = implode(',', array_column($cate,'cate_id'));

            $where = '1=1';
            // if ($type) {
            //     $where .= " and type=$type";
            // }
            // if ($level) {
            //     $where .= " and level=$level";
            // }
            // if ($topic) {
            //     $where .= " and topic=$topic";
            // }
            if ($cate_str) {
                $where .= " and cate_id in($cate_str)";
            }
            if($cate_id){
                $where .= " and cate_id = $cate_id";
            }
            // f_d($where);
            $model = new ExerciseList();
            $data = $model->getData($where, 15, $page);
            // f_d($data);
            return $this->render('index',[
                'data' => $data,
                'cate'=>$cate,
                'cate_id'=>$cate_id
                // 'option' => $option,
                // 'type' => $type,
                // 'level' => $level,
                // 'topic' => $topic,
                // 'block' => $this->block
            ]);
        }

        /**
         * [actionUpdate 修改]
         * @Title  [title]
         * @Author Don
         * @Date   2021-05-11
         * @return [type]     [description]
         */
        public function actionUpdate()
        {
            if ($_POST) {
                // f_d($_POST);
                $id = Yii::$app->request->post('id');
                $name = Yii::$app->request->post('name');
                $title = Yii::$app->request->post('title');
                $title_image = Yii::$app->request->post('title_image');
                $image = Yii::$app->request->post('image');
                $description = Yii::$app->request->post('description');
                $viewCount = Yii::$app->request->post('viewCount');
                $cate_id = Yii::$app->request->post('cate_id');
                if($id==false){
                    $data['cate_id'] = $cate_id;
                    $data['description'] = $description;
                    $tem=ExerciseCategory::find()->asArray()->where('cate_id='.$data['cate_id'])->one();
                    $content1=ExerciseList::find()->asArray()->orderBy('content_id desc')->one();
                    $content2=ExerciseListDetail::find()->asArray()->orderBy('content_id desc')->one();
                    $data['content_id']=max($content1['content_id'],$content2['content_id'])+1;
                    $data['catName']= $tem['name'];
                    $data['name'] = $name;
                    $data['title'] = $title;
                    $data['title_image'] = $title_image;
                    $data['image'] = $image;
                    $data['viewCount'] = $viewCount;
                    $data['createTime'] = date('Y-m-d H:i:s',time());
                    $data['pid'] = 0;
                    $data['sort'] = $data['content_id'];
                    $data['show'] = 1;
                    $data['userId'] = Yii::$app->session->get('adminId');
                    $res = Yii::$app->db->createCommand()->insert("{{%exercise_list}}", $data)->execute();
                }else{
                    $model = ExerciseList::findOne($id);
                    $model->name = $name;
                    $model->title = $title;
                    $model->title_image = $title_image;
                    $model->image = $image;
                    $model->description = $description;
                    $model->viewCount = $viewCount;
                    $res = $model->save();
                }
                if ($res) {
                    $this->redirect('/exercise/write/index?cate_id='.$cate_id);
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }
            } else {
                $id = Yii::$app->request->get('id');
                $data = ExerciseList::findOne($id);
                $cate_model = new ExerciseCategory();
                $datalist = $cate_model->getAllCates(512);
                // f_d($data);
                return $this->render('update',[
                    'data' => $data,
                    'id' => $id,
                    'datalist'=>$datalist
                ]);
            }
            
        }

        /**
         * [详情]
         * @Title  [title]
         * @Author Don
         * @Date   2021-05-11
         * @return [type]     [description]
         */
        public function actionUpdateDetail()
        {
            $cate_id = Yii::$app->request->get('cate_id');
            if ($_POST) {
                $id = Yii::$app->request->post('id');
                $description = Yii::$app->request->post('description');
                $alternatives = Yii::$app->request->post('alternatives');
                $analysis = Yii::$app->request->post('analysis');
                if($id==false){
                    $pid= Yii::$app->request->post('pid');
                    $data['description'] = $description;
                    $data['alternatives'] = $alternatives;
                    $data['createTime'] = date('Y-m-d H:i:s',time());
                    $contentid= ExerciseList::find()->asArray()->where('id='.$pid)->one();
                    $tem=ExerciseCategory::find()->asArray()->where('cate_id='.$contentid['cate_id'])->one();
                    $content1=ExerciseList::find()->asArray()->orderBy('content_id desc')->one();
                    $content2=ExerciseListDetail::find()->asArray()->orderBy('content_id desc')->one();
                    $data['content_id']=max($content1['content_id'],$content2['content_id'])+1;
                    $data['catName']= $tem['name'];
                    $contentid= ExerciseList::find()->asArray()->where('id='.$pid)->one();
                    $data['pid']= $contentid['content_id'];
                    $data['cate_id']= $contentid['cate_id'];
                    $data['name']= '写作阅读题';
                    $data['sort'] = $data['content_id'];
                    $data['userId'] = Yii::$app->session->get('adminId');
                    $data['show'] = 1;
                    $data['analysis'] = $analysis;
                    $res = Yii::$app->db->createCommand()->insert("{{%exercise_list_detail}}", $data)->execute();
                }else{
                    $model = ExerciseListDetail::findOne($id);
                    $model->description = $description;
                    $model->alternatives = $alternatives;
                    $model->analysis = $analysis;
                    $res = $model->save();
                }

                if ($res) {
                    $this->redirect('/exercise/write/index?cate_id='.$cate_id);
                } else {
                     die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }
            } else {
                $id = Yii::$app->request->get('id');
                $list = ExerciseList::findOne($id);
                // f_d($list);
                $pid = $list->content_id;
                $data = ExerciseListDetail::find()->where(['pid' => $pid])->asArray()->one();
                // f_d($data);
                if($data==false){
                    $data['catName']=$list['catName'];
                    return $this->render('update_detail',[
                        'cate_id'=>$cate_id,
                        'data' => $data,
                        'id' => ''
                    ]);
                }
                return $this->render('update_detail',[
                    'cate_id'=>$cate_id,
                    'data' => $data,
                    'id' => $data['id']
                ]);
            }
        }

        public function actionAdd()
        {
            $cate_id = Yii::$app->request->get('cate_id');
            $cate_model = new ExerciseCategory();
            $datalist = $cate_model->getAllCates(512);//分类
            return $this->render('update',[
                'data' => '',
                'id' => '',
                'datalist'=>$datalist,
                'cate_id'=>$cate_id
            ]);
        }
}